/* * The MIT License
 *
 * Copyright (c) 2010 Dark Crimson, aka DCRM
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */


package jkiskeyblocker;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import java.util.List;
import javax.swing.JOptionPane;

public class DataChanger implements Runnable  {
    private String pwd;
    private String id;
    private String email;
    private String name;
    private String sity;
    private Boolean change;

    private Thread thread;
    private String host="https://support.kaspersky.com/ru/personalcabinet/edit_data";

    public DataChanger(String id, String pwd, String email, String name, String sity,Boolean change) {
        this.id=id;
        this.pwd=pwd;
        this.email=email;
        this.name=name;
        this.sity=sity;
        this.change=change;
        thread = new Thread(this);
        thread.setName("DataChanger");
        thread.start();
    }
    public DataChanger(String id, String pwd, String email, String name) {
        this(id, pwd, email, name, "",true);
    }
    public DataChanger(String id, String pwd, String email) {
        this(id, pwd, email, "", "",true);
    }

    public void run() {
        try {
            WebClient webClient = new WebClient();
            webClient.setJavaScriptEnabled(false);
            webClient.setCssEnabled(false);
            
            HtmlPage htmlPage0 = webClient.getPage(host);
            List<HtmlForm> htmlForm = htmlPage0.getForms();
            htmlForm.get(1).getInputByName("LOGIN").setValueAttribute(id);
            htmlForm.get(1).getInputByName("PASSWD").setValueAttribute(pwd);
            HtmlPage htmlPage1 = htmlForm.get(1).getInputByName("accept_auth").click();

            List<HtmlForm> htmlForm1 = htmlPage1.getForms();
            String old_eMail=htmlForm1.get(1).getInputByName("f_email").getDefaultValue();
            htmlForm1.get(1).getInputByName("f_email").setValueAttribute(email);

            String old_Name=htmlForm1.get(1).getInputByName("f_nik").getDefaultValue();
            if (!name.isEmpty()) htmlForm1.get(1).getInputByName("f_nik").setValueAttribute(name);
            
            String old_Sity=htmlForm1.get(1).getInputByName("f_city").getDefaultValue();
            if (!sity.isEmpty()) htmlForm1.get(1).getInputByName("f_city").setValueAttribute(sity);
            if (change) htmlForm1.get(1).getInputByName("send").click();
            webClient.closeAllWindows();

            JOptionPane.showMessageDialog(null, ((change)?"Change":"View"+" info:")+"\n"
                    +" * eMail: "+old_eMail+" -> "+email+"\n"
                    +" * Name: "+((!name.isEmpty() || !change)?(old_Name+" -> "+name+"\n"):"")
                    +" * Sity: "+((!sity.isEmpty() || !change)?(old_Sity+" -> "+sity+"\n"):"")
                    + "", ((change)?"Change":"View"+" - OK :)") , JOptionPane.INFORMATION_MESSAGE);
            MainFrame.reset_b();
        } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Error Login! or ...\n"+ex.getLocalizedMessage(), "Error!", JOptionPane.ERROR_MESSAGE); MainFrame.reset_b();}
    }

}
